package com.ruoyi.system.service.impl;

import java.util.*;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.ShiwuMapper;
import com.ruoyi.system.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RenwuMapper;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-22
 */
@Service
public class RenwuServiceImpl implements IRenwuService 
{
    private String ZJZXDeptId = "0";
    @Autowired
    private RenwuMapper renwuMapper;

    @Autowired
    private ShiwuMapper shiwuMapper;

    @Autowired
    private IRenwuService renwuService;

    @Autowired
    private IZhuanjiaBiandongDetailService zhuanjiaBiandongDetailService;

    @Autowired
    private IZhuanjiaService zhuanjiaService;

    @Autowired
    private IDeptZhuanjiaService deptZhuanjiaService;

    @Autowired
    private IDeptidOlddeptidService deptidOlddeptidService;

    @Autowired
    private IZhuanjiaBiandongHistoryService zhuanjiaBiandongHistoryService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IZhuanjiaQushiquerenService zhuanjiaQushiquerenService;

    @Autowired
    private  IZhuanjiaQushiquerenPiciService zhuanjiaQushiquerenPiciService;
    /**
     * 查询任务
     * 
     * @param renwuid 任务主键
     * @return 任务
     */
    @Override
    public Renwu selectRenwuByRenwuid(String renwuid)
    {
        return renwuMapper.selectRenwuByRenwuid(renwuid);
    }

    /**
     * 查询任务列表
     * 
     * @param renwu 任务
     * @return 任务
     */
    @Override
    public List<Renwu> selectRenwuList(Renwu renwu)
    {
        return renwuMapper.selectRenwuList(renwu);
    }

    /**
     * 新增任务
     * 
     * @param renwu 任务
     * @return 结果
     */
    @Override
    public int insertRenwu(Renwu renwu)
    {
        return renwuMapper.insertRenwu(renwu);
    }

    /**
     * 修改任务
     * 
     * @param renwu 任务
     * @return 结果
     */
    @Override
    public int updateRenwu(Renwu renwu)
    {
        return renwuMapper.updateRenwu(renwu);
    }

    /**
     * 批量删除任务
     * 
     * @param renwuids 需要删除的任务主键
     * @return 结果
     */
    @Override
    public int deleteRenwuByRenwuids(String renwuids)
    {
        return renwuMapper.deleteRenwuByRenwuids(Convert.toStrArray(renwuids));
    }

    /**
     * 删除任务信息
     * 
     * @param renwuid 任务主键
     * @return 结果
     */
    @Override
    public int deleteRenwuByRenwuid(String renwuid)
    {
        return renwuMapper.deleteRenwuByRenwuid(renwuid);
    }

    @Override
    @Transactional
    public boolean submit(Renwu ppRenwu, Shiwu ppShiwu){
        boolean ok = true;

        int addRenwu = this.insertRenwu(ppRenwu);
        if (addRenwu!=1){
            ok = false;
            throw new RuntimeException("添加任务失败");
        }

        int modifyShiwu = shiwuMapper.updateShiwu(ppShiwu);
        if (modifyShiwu!=1){
            ok = false;
            throw new RuntimeException("修改事务失败");
        }

        String mmZhuanjiabiandongpiciId = ppShiwu.getBeizhu1();
        ZhuanjiaBiandongDetail mmZhuanjiaBiandongDetail = new ZhuanjiaBiandongDetail();
        mmZhuanjiaBiandongDetail.setBiandongpiciid(mmZhuanjiabiandongpiciId);

        List<ZhuanjiaBiandongDetail> mmList = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailList(mmZhuanjiaBiandongDetail);
        for (int i=0;i<mmList.size();i++){
            ZhuanjiaBiandongDetail mmDetail = mmList.get(i);
            mmDetail.setBaosongshijian(new Date());
            int modifyDetail = zhuanjiaBiandongDetailService.updateZhuanjiaBiandongDetail(mmDetail);
            if (modifyDetail!=1){
                ok = false;
                throw new RuntimeException("修改详情失败");
            }
        }

        return ok;
    }

    @Override
    @Transactional
    public boolean submit_custom(Renwu ppRenwu, Shiwu ppShiwu,ZhuanjiaBiandongDetail ppZhuanjiabiandongDetail){
        boolean ok = true;

        //初始化一个事务ID
        String mmShiwuId = UUID.randomUUID().toString();
        ppShiwu.setShiwuid(mmShiwuId);

        //根据变动ID查询有无事务
        String mmId = ppShiwu.getBeizhu1();
        Shiwu mmShiwu = new Shiwu();
        mmShiwu.setBeizhu1(mmId);

        List<Shiwu> mmShiwuList = shiwuMapper.selectShiwuList(mmShiwu);
        if (mmShiwuList.size()==0){//无事务，创建新事务

            ppRenwu.setShiwuid(mmShiwuId);
            ppRenwu.setOrdernum(findMaxOrdernumByShiwuId(mmShiwuId)+1);

            int insertShiwu = shiwuMapper.insertShiwu(ppShiwu);
            if (insertShiwu!=1){
                ok = false;
                throw new RuntimeException("添加事务失败");
            }

        }else{//有事务，修改

            String shiwuid = mmShiwuList.get(0).getShiwuid();
            mmShiwu.setShiwuid(shiwuid);
            mmShiwu.setIsqidong((long) 1);
            mmShiwu.setIsjieshu((long) 0);
            mmShiwu.setQidongshijian(new Date());
            mmShiwu.setBeizhu1(mmId);
            mmShiwu.setIsvalid((long) 1);

            ppRenwu.setShiwuid(shiwuid);
            ppRenwu.setOrdernum(findMaxOrdernumByShiwuId(mmShiwu.getShiwuid())+1);

            int modifyShiwu = shiwuMapper.updateShiwu(mmShiwu);
            if (modifyShiwu!=1){
                ok = false;
                throw new RuntimeException("修改事务失败");
            }
        }

        int addRenwu = this.insertRenwu(ppRenwu);
        if (addRenwu!=1){
            ok = false;
            throw new RuntimeException("添加任务失败");
        }

        ppZhuanjiabiandongDetail.setBaosongshijian(new Date());
        int modifyDetail = zhuanjiaBiandongDetailService.updateZhuanjiaBiandongDetail(ppZhuanjiabiandongDetail);
        if (modifyDetail!=1){
            ok = false;
            throw new RuntimeException("修改详情失败");
        }

        ZhuanjiaQushiqueren mmQushiTemp = new ZhuanjiaQushiqueren();
        mmQushiTemp.setShifouchuli((long)0);
        mmQushiTemp.setBiandongid(ppZhuanjiabiandongDetail.getId());
        List<ZhuanjiaQushiqueren>mmQushiListTemp = zhuanjiaQushiquerenService.selectZhuanjiaQushiquerenList(mmQushiTemp);
        if (mmQushiListTemp.size()==1){
            ZhuanjiaQushiqueren mmQushiQueren = mmQushiListTemp.get(0);
            mmQushiQueren.setShifouchuli((long)1);
            mmQushiQueren.setChuliriqi(new Date());
            String str = "于"+new Date()+"提交 "+ppZhuanjiabiandongDetail.getZhuanjiamingcheng()+"的变动申报";
            mmQushiQueren.setChulijieguo(str);
            mmQushiQueren.setShuoming(ppZhuanjiabiandongDetail.getShuoming());
            int modifyQushiqueren = zhuanjiaQushiquerenService.updateZhuanjiaQushiqueren(mmQushiQueren);
            if (modifyQushiqueren!=1){
                ok = false;
                throw new RuntimeException("修改去世确认失败");
            }

            ZhuanjiaQushiquerenPici mmPici = zhuanjiaQushiquerenPiciService.selectZhuanjiaQushiquerenPiciById(mmQushiQueren.getPiciid());
            mmPici.setWeihecha(mmPici.getWeihecha()-1);
            mmPici.setYihecha(mmPici.getYihecha()+1);

            int modifyPici = zhuanjiaQushiquerenPiciService.updateZhuanjiaQushiquerenPici(mmPici);
            if (modifyPici!=1){
                ok = false;
                throw new RuntimeException("修改批次失败");
            }
        }

        return ok;
    }

    @Override
    @Transactional
    public boolean shenhe(ZhuanjiaBiandongDetail ppZhuanjiabiandongDetail,Renwu ppRenwu1,Renwu ppRenwu2,Shiwu ppShiwu){
        boolean ok = true;

        int modifyRenwu = this.updateRenwu(ppRenwu1);
        if (modifyRenwu!=1){
            ok = false;
            throw new RuntimeException("修改任务失败");
        }

        long mmShifoutongguo = ppRenwu1.getShifoutongguo();
        String mmDeptId = ppRenwu1.getShenpidanweiid();

        //非专家中心审核通过，继续下一个任务
        if (mmShifoutongguo==1&&!mmDeptId.equals(RuoYiConfig.getZjzxdeptid())){

            int addRenwu = this.insertRenwu(ppRenwu2);
            if (addRenwu!=1){
                ok = false;
                throw new RuntimeException("添加任务失败");
            }

        }

        //专家中心审核通过
        if (mmShifoutongguo==1&&mmDeptId.equals(RuoYiConfig.getZjzxdeptid())){

            ZhuanjiaBiandongDetail mmDetail = ppZhuanjiabiandongDetail;
            mmDetail.setShenpishijian(new Date());
            if (mmDetail.getBeizhu1()==null){
                mmDetail.setBeizhu1("1");//通过标记
            }
            int modifyDetail = zhuanjiaBiandongDetailService.updateZhuanjiaBiandongDetail(mmDetail);
            if (modifyDetail!=1){
                ok = false;
                throw new RuntimeException("修改详情失败");
            }

            //只修改单个审批通过的
            if(mmDetail.getBeizhu1().equals("1")){

                Zhuanjia mmZhuanjia = zhuanjiaService.selectZhuanjiaByZhuanjiaid(mmDetail.getZhuanjiaid());

                ZhuanjiaBiandongHistory mmHistory = new ZhuanjiaBiandongHistory();
                mmHistory.setId(UUID.randomUUID().toString());
                mmHistory.setZhuanjiaid(mmDetail.getZhuanjiaid());

                if (mmZhuanjia.getZhuanjiazhuangtai()==null){
                    mmHistory.setOldzhuantai("");
                }else{
                    SysDictData mmDictOldZhuangtai = new SysDictData();
                    mmDictOldZhuangtai.setDictType("sys_zhuanjiazhuangtai");
                    mmDictOldZhuangtai.setDictValue(mmZhuanjia.getZhuanjiazhuangtai());

                    mmHistory.setOldzhuantai(sysDictDataService.selectDictDataList(mmDictOldZhuangtai).get(0).getDictLabel());

                }
                SysDictData mmDictNewZhuangtai = new SysDictData();
                mmDictNewZhuangtai.setDictType("sys_zhuanjiazhuangtai");
                mmDictNewZhuangtai.setDictValue(mmDetail.getBiandongleixing());

                mmHistory.setNewzhuangtai(sysDictDataService.selectDictDataList(mmDictNewZhuangtai).get(0).getDictLabel());
                mmHistory.setBiandongshijian(mmDetail.getBiandongriqi());
                mmHistory.setOrdernum(zhuanjiaBiandongHistoryService.getMaxOrdernum(mmDetail.getZhuanjiaid())+1);
                mmHistory.setIsvalid((long) 1);

                int addHistory = zhuanjiaBiandongHistoryService.insertZhuanjiaBiandongHistory(mmHistory);
                if (addHistory!=1){
                    ok = false;
                    throw new RuntimeException("添加专家变动历史失败");
                }

                if(mmDetail.getBeizhu3()!=null){
                    mmZhuanjia.setGongzuodanwei(mmDetail.getBeizhu3());
                }

                mmZhuanjia.setZhuanjiazhuangtai(mmDetail.getBiandongleixing());
                mmZhuanjia.setZhuangtaibiandongshijian(mmDetail.getBiandongriqi());
                if (mmDetail.getBiandongleixing().equals("5")){
                    mmZhuanjia.setQushishijian(mmDetail.getBiandongriqi());
                }
                int modifyZhuanjia = zhuanjiaService.updateZhuanjia(mmZhuanjia);
                if (modifyZhuanjia!=1){
                    ok = false;
                    throw new RuntimeException("修改专家失败");
                }

            }

        }

        int modifyShiwu = shiwuMapper.updateShiwu(ppShiwu);
        if (modifyShiwu!=1){
            throw new RuntimeException("修改事务失败");
        }

        return ok;
    }

//    @Override
//    @Transactional
//    public boolean shenhe_zhuanli(ZhuanjiaBiandongDetail ppZhuanjiabiandongDetail,Renwu ppRenwu1,Renwu ppRenwu2,Shiwu ppShiwu){
//        boolean ok = true;
//
//        int modifyRenwu = this.updateRenwu(ppRenwu1);
//
//        if (modifyRenwu!=1){
//            ok = false;
//            throw new RuntimeException("修改任务失败");
//        }
//
//        long mmShifoutongguo = ppRenwu1.getShifoutongguo();
//        String mmDeptId = ppRenwu1.getShenpidanweiid();
//        String mmOldDeptId = deptidToOlddeptid(mmDeptId);
//
//        //审核单位不为转至单位时，继续创建下一个任务
//        if (mmShifoutongguo==1&&!mmOldDeptId.equals(ppZhuanjiabiandongDetail.getZhuanzhidanwei())){
//
//            int addRenwu = this.insertRenwu(ppRenwu2);
//            if (addRenwu!=1){
//                ok = false;
//                throw new RuntimeException("添加任务失败");
//            }
//        }
//
//        //若审核单位是转至单位，并且通过，结束该事务
//        if (mmShifoutongguo==1&&mmOldDeptId.equals(ppZhuanjiabiandongDetail.getZhuanzhidanwei())){
//
//            ppZhuanjiabiandongDetail.setShenpishijian(new Date());
//            ppZhuanjiabiandongDetail.setBeizhu1("1");//通过标记
//            int modifyDetail = zhuanjiaBiandongDetailService.updateZhuanjiaBiandongDetail(ppZhuanjiabiandongDetail);
//            if (modifyDetail!=1){
//                ok = false;
//                throw new RuntimeException("修改详情失败");
//            }
//            Zhuanjia mmZhuanjia = zhuanjiaService.selectZhuanjiaByZhuanjiaid(ppZhuanjiabiandongDetail.getZhuanjiaid());
//            //mmZhuanjia.setZhuanjiazhuangtai(ppZhuanjiabiandongDetail.getBiandongleixing());//专家为转入，状态正常不变
//            mmZhuanjia.setZhuangtaibiandongshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
//            mmZhuanjia.setZhuanjiazhuangtai("11");
//
//            int modifyZhuanjia = zhuanjiaService.updateZhuanjia(mmZhuanjia);
//            if (modifyZhuanjia!=1){
//                ok = false;
//                throw new RuntimeException("修改专家失败");
//            }
//
//            DeptZhuanjia mmDeptZhuanjia = deptZhuanjiaService.findByZhuanjiaId(ppZhuanjiabiandongDetail.getZhuanjiaid());
//
//            //String mmZhuanzhiDeptId = olddeptidToDeptid(ppZhuanjiabiandongDetail.getZhuanzhidanwei());
//            //mmDeptZhuanjia.setDeptid(ppZhuanjiabiandongDetail.getZhuanzhidanwei());
//
//            mmDeptZhuanjia.setBiangengshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
//            mmDeptZhuanjia.setIsvalid((long)2);
//
//            int modifyDeptZhuanjia = deptZhuanjiaService.updateDeptZhuanjia(mmDeptZhuanjia);
//            if (modifyDeptZhuanjia!=1){
//                ok = false;
//                throw new RuntimeException("修改单位专家失败");
//            }
//
//            DeptZhuanjia mmNewDeptZhuanjia = new DeptZhuanjia();
//            mmNewDeptZhuanjia.setId(UUID.randomUUID().toString());
//            mmNewDeptZhuanjia.setZhuanjiaid(ppZhuanjiabiandongDetail.getZhuanjiaid());
//            mmNewDeptZhuanjia.setDeptid(ppZhuanjiabiandongDetail.getZhuanzhidanwei());
//            mmNewDeptZhuanjia.setXingming(mmZhuanjia.getXingming());
//            mmNewDeptZhuanjia.setBiangengshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
//            mmNewDeptZhuanjia.setIsvalid((long)1);
//
//            int addNewDeptZhuanjia = deptZhuanjiaService.insertDeptZhuanjia(mmNewDeptZhuanjia);
//            if (addNewDeptZhuanjia!=1){
//                ok = false;
//                throw new RuntimeException("添加新单位专家失败");
//            }
//
//            ZhuanjiaBiandongHistory mmHistory = new ZhuanjiaBiandongHistory();
//            mmHistory.setId(UUID.randomUUID().toString());
//            mmHistory.setZhuanjiaid(ppZhuanjiabiandongDetail.getZhuanjiaid());
//
//            if (mmZhuanjia.getZhuanjiazhuangtai()==null){
//                mmHistory.setOldzhuantai("");
//            }else{
//                SysDictData mmDictOldZhuangtai = new SysDictData();
//                mmDictOldZhuangtai.setDictType("sys_zhuanjiazhuangtai");
//                mmDictOldZhuangtai.setDictValue("10");
//                mmHistory.setOldzhuantai(sysDictDataService.selectDictDataList(mmDictOldZhuangtai).get(0).getDictLabel());
//            }
//            SysDictData mmDictNewZhuangtai = new SysDictData();
//            mmDictNewZhuangtai.setDictType("sys_zhuanjiazhuangtai");
//            mmDictNewZhuangtai.setDictValue("11");
//            mmHistory.setNewzhuangtai(sysDictDataService.selectDictDataList(mmDictNewZhuangtai).get(0).getDictLabel());
//            mmHistory.setOlddanwei(deptService.selectDeptById(ppZhuanjiabiandongDetail.getZhuanzidanwei()).getJigoumingcheng());
//            mmHistory.setNewdanwei(deptService.selectDeptById(ppZhuanjiabiandongDetail.getZhuanzhidanwei()).getJigoumingcheng());
//            mmHistory.setBiandongshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
//            mmHistory.setOrdernum(zhuanjiaBiandongHistoryService.getMaxOrdernum(ppZhuanjiabiandongDetail.getZhuanjiaid())+1);
//            mmHistory.setIsvalid((long) 1);
//
//            int addHistory = zhuanjiaBiandongHistoryService.insertZhuanjiaBiandongHistory(mmHistory);
//            if (addHistory!=1){
//                ok = false;
//                throw new RuntimeException("添加专家变动历史失败");
//            }
//
//            ZhuanjiaBiandongDetail mmZhuanjiabiandongDetail = new ZhuanjiaBiandongDetail();
//            mmZhuanjiabiandongDetail.setId(UUID.randomUUID().toString());
//            mmZhuanjiabiandongDetail.setDeptid(mmDeptId);
//            mmZhuanjiabiandongDetail.setZhuanjiaid(ppZhuanjiabiandongDetail.getZhuanjiaid());
//            mmZhuanjiabiandongDetail.setBiandongriqi(ppZhuanjiabiandongDetail.getBiandongriqi());
//            mmZhuanjiabiandongDetail.setZhuanjiamingcheng("专家转入");
//            mmZhuanjiabiandongDetail.setShenpishijian(new Date());
//            mmZhuanjiabiandongDetail.setBeizhu1("1");
//            mmZhuanjiabiandongDetail.setIsused((long)0);
//            mmZhuanjiabiandongDetail.setIsvalid((long)1);
//
//            int addBiandongDetail = zhuanjiaBiandongDetailService.insertZhuanjiaBiandongDetailCustom(mmZhuanjiabiandongDetail)==true?1:0;
//            if (addBiandongDetail!=1){
//                ok = false;
//                throw new RuntimeException("添加专家变动详情失败");
//            }
//        }
//
//        int modifyShiwu = shiwuMapper.updateShiwu(ppShiwu);
//        if (modifyShiwu!=1){
//            throw new RuntimeException("修改事务失败");
//        }
//
//        return ok;
//    }

//    @Override
//    @Transactional
//    public boolean shenhe_zhuanli(ZhuanjiaBiandongDetail ppZhuanjiabiandongDetail,Renwu ppRenwu,Shiwu ppShiwu){
//        boolean ok = true;
//
//        int modifyRenwu = this.updateRenwu(ppRenwu);
//
//        if (modifyRenwu!=1){
//            ok = false;
//            throw new RuntimeException("修改任务失败");
//        }
//
//        long mmShifoutongguo = ppRenwu.getShifoutongguo();
//        String mmDeptId = ppRenwu.getShenpidanweiid();
//        String mmOldDeptId = deptidToOlddeptid(mmDeptId);
//
//        //审核单位不为转至单位时，继续创建下一个任务
////        if (mmShifoutongguo==1&&!mmOldDeptId.equals(ppZhuanjiabiandongDetail.getZhuanzhidanwei())){
////
////            int addRenwu = this.insertRenwu(ppRenwu2);
////            if (addRenwu!=1){
////                ok = false;
////                throw new RuntimeException("添加任务失败");
////            }
////        }
//
//        //若审核单位是转至单位，并且通过，结束该事务
//        if (mmShifoutongguo==1&&mmOldDeptId.equals(ppZhuanjiabiandongDetail.getZhuanzhidanwei())){
//
//            ppZhuanjiabiandongDetail.setShenpishijian(new Date());
//            ppZhuanjiabiandongDetail.setBeizhu1("1");//通过标记
//            int modifyDetail = zhuanjiaBiandongDetailService.updateZhuanjiaBiandongDetail(ppZhuanjiabiandongDetail);
//            if (modifyDetail!=1){
//                ok = false;
//                throw new RuntimeException("修改详情失败");
//            }
//
//
//            Zhuanjia mmZhuanjia = zhuanjiaService.selectZhuanjiaByZhuanjiaid(ppZhuanjiabiandongDetail.getZhuanjiaid());
//
//            //mmZhuanjia.setZhuanjiazhuangtai(ppZhuanjiabiandongDetail.getBiandongleixing());//专家为转入，状态正常不变
//            mmZhuanjia.setZhuangtaibiandongshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
//            mmZhuanjia.setZhuanjiazhuangtai("8");
//
//            int modifyZhuanjia = zhuanjiaService.updateZhuanjia(mmZhuanjia);
//            if (modifyZhuanjia!=1){
//                ok = false;
//                throw new RuntimeException("修改专家失败");
//            }
//
//            Zhuanjia mmNewZhuanjia = mmZhuanjia;
//            String mmZhuanjiaId = UUID.randomUUID().toString();
//            mmNewZhuanjia.setZhuanjiaid(mmZhuanjiaId);
//            mmNewZhuanjia.setZhuanjiazhuangtai("11");
//            mmNewZhuanjia.setZhuangtaibiandongshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
//
//            int addZhuanjia = zhuanjiaService.insertZhuanjia(mmNewZhuanjia);
//            if (addZhuanjia!=1){
//                ok = false;
//                throw new RuntimeException("添加专家失败");
//            }
//
//            DeptZhuanjia mmDeptZhuanjia = deptZhuanjiaService.findByZhuanjiaId(ppZhuanjiabiandongDetail.getZhuanjiaid());
//
//            //String mmZhuanzhiDeptId = olddeptidToDeptid(ppZhuanjiabiandongDetail.getZhuanzhidanwei());
//            //mmDeptZhuanjia.setDeptid(ppZhuanjiabiandongDetail.getZhuanzhidanwei());
//
//            mmDeptZhuanjia.setBiangengshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
//            mmDeptZhuanjia.setIsvalid((long)2);
//
//            int modifyDeptZhuanjia = deptZhuanjiaService.updateDeptZhuanjia(mmDeptZhuanjia);
//            if (modifyDeptZhuanjia!=1){
//                ok = false;
//                throw new RuntimeException("修改单位专家失败");
//            }
//
//            DeptZhuanjia mmNewDeptZhuanjia = new DeptZhuanjia();
//            mmNewDeptZhuanjia.setId(UUID.randomUUID().toString());
//            mmNewDeptZhuanjia.setZhuanjiaid(mmZhuanjiaId);
//            mmNewDeptZhuanjia.setDeptid(ppZhuanjiabiandongDetail.getZhuanzhidanwei());
//            mmNewDeptZhuanjia.setXingming(mmZhuanjia.getXingming());
//            mmNewDeptZhuanjia.setBiangengshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
//            mmNewDeptZhuanjia.setIsvalid((long)1);
//
//            int addNewDeptZhuanjia = deptZhuanjiaService.insertDeptZhuanjia(mmNewDeptZhuanjia);
//            if (addNewDeptZhuanjia!=1){
//                ok = false;
//                throw new RuntimeException("添加新单位专家失败");
//            }
//
//            ZhuanjiaBiandongHistory mmHistory = new ZhuanjiaBiandongHistory();
//            mmHistory.setId(UUID.randomUUID().toString());
//            mmHistory.setZhuanjiaid(ppZhuanjiabiandongDetail.getZhuanjiaid());
//
//            if (mmZhuanjia.getZhuanjiazhuangtai()==null){
//                mmHistory.setOldzhuantai("");
//            }else{
//                SysDictData mmDictOldZhuangtai = new SysDictData();
//                mmDictOldZhuangtai.setDictType("sys_zhuanjiazhuangtai");
//                mmDictOldZhuangtai.setDictValue("10");
//                mmHistory.setOldzhuantai(sysDictDataService.selectDictDataList(mmDictOldZhuangtai).get(0).getDictLabel());
//            }
//            SysDictData mmDictNewZhuangtai = new SysDictData();
//            mmDictNewZhuangtai.setDictType("sys_zhuanjiazhuangtai");
//            mmDictNewZhuangtai.setDictValue("11");
//            mmHistory.setNewzhuangtai(sysDictDataService.selectDictDataList(mmDictNewZhuangtai).get(0).getDictLabel());
//            mmHistory.setOlddanwei(deptService.selectDeptById(ppZhuanjiabiandongDetail.getZhuanzidanwei()).getJigoumingcheng());
//            mmHistory.setNewdanwei(deptService.selectDeptById(ppZhuanjiabiandongDetail.getZhuanzhidanwei()).getJigoumingcheng());
//            mmHistory.setBiandongshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
//            mmHistory.setOrdernum(zhuanjiaBiandongHistoryService.getMaxOrdernum(ppZhuanjiabiandongDetail.getZhuanjiaid())+1);
//            mmHistory.setIsvalid((long) 1);
//
//            int addHistory = zhuanjiaBiandongHistoryService.insertZhuanjiaBiandongHistory(mmHistory);
//            if (addHistory!=1){
//                ok = false;
//                throw new RuntimeException("添加专家变动历史失败");
//            }
//
//            ZhuanjiaBiandongDetail mmZhuanjiabiandongDetail = new ZhuanjiaBiandongDetail();
//            mmZhuanjiabiandongDetail.setId(UUID.randomUUID().toString());
//            mmZhuanjiabiandongDetail.setDeptid(mmDeptId);
//            mmZhuanjiabiandongDetail.setZhuanjiaid(ppZhuanjiabiandongDetail.getZhuanjiaid());
//            mmZhuanjiabiandongDetail.setBiandongriqi(ppZhuanjiabiandongDetail.getBiandongriqi());
//            mmZhuanjiabiandongDetail.setZhuanjiamingcheng("专家转入");
//            mmZhuanjiabiandongDetail.setShenpishijian(new Date());
//            mmZhuanjiabiandongDetail.setBeizhu1("1");
//            mmZhuanjiabiandongDetail.setIsused((long)0);
//            mmZhuanjiabiandongDetail.setIsvalid((long)1);
//
//            int addBiandongDetail = zhuanjiaBiandongDetailService.insertZhuanjiaBiandongDetailCustom(mmZhuanjiabiandongDetail)==true?1:0;
//            if (addBiandongDetail!=1){
//                ok = false;
//                throw new RuntimeException("添加专家变动详情失败");
//            }
//        }
//
//        int modifyShiwu = shiwuMapper.updateShiwu(ppShiwu);
//        if (modifyShiwu!=1){
//            throw new RuntimeException("修改事务失败");
//        }
//
//        return ok;
//    }

    @Override
    @Transactional
    public boolean shenhe_zhuanli(ZhuanjiaBiandongDetail ppZhuanjiabiandongDetail,Renwu ppRenwu,Renwu ppRenwu2,Shiwu ppShiwu,int ppIsRenling,Dept ppDept){
        boolean ok = true;

        //修改本次任务
        int modifyRenwu = this.updateRenwu(ppRenwu);
        if (modifyRenwu!=1){
            ok = false;
            throw new RuntimeException("修改任务失败");
        }

        //是否通过
        long mmShifoutongguo = ppRenwu.getShifoutongguo();
        //当前审批单位
        String mmDeptId = ppRenwu.getShenpidanweiid();
        //当前审批单位旧ID
        String mmOldDeptId = ppDept.getId();
        //转出单位旧ID
        String mmZhuanzidanweiId = ppZhuanjiabiandongDetail.getZhuanzidanwei();
        //转出单位新ID

        //转入单位旧ID
        String mmZhuanzhidanweiId = ppZhuanjiabiandongDetail.getZhuanzhidanwei()==null?"":ppZhuanjiabiandongDetail.getZhuanzhidanwei();
        //转入单位新ID

        //转入单位上级旧ID
        String mmZhuanzhidanweiShangjiId = "";


        //若选择认领，则结束事务，不创建下一任务，专家直接转入本单位
        if (ppIsRenling==1){
            ppZhuanjiabiandongDetail.setShenpishijian(new Date());
            ppZhuanjiabiandongDetail.setBeizhu1("1");//通过标记

            Zhuanjia mmZhuanjia = zhuanjiaService.selectZhuanjiaByZhuanjiaid(ppZhuanjiabiandongDetail.getZhuanjiaid());

            mmZhuanjia.setZhuangtaibiandongshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
            mmZhuanjia.setZhuanjiazhuangtai("8");//正常

            int modifyZhuanjia = zhuanjiaService.updateZhuanjia(mmZhuanjia);
            if (modifyZhuanjia!=1){
                ok = false;
                throw new RuntimeException("修改专家失败");
            }

            Zhuanjia mmNewZhuanjia = mmZhuanjia;
            String mmZhuanjiaId = UUID.randomUUID().toString();
            mmNewZhuanjia.setZhuanjiaid(mmZhuanjiaId);
            mmNewZhuanjia.setZhuanjiazhuangtai("11");
            mmNewZhuanjia.setZhuangtaibiandongshijian(ppZhuanjiabiandongDetail.getBiandongriqi());

            int addZhuanjia = zhuanjiaService.insertZhuanjia(mmNewZhuanjia);
            if (addZhuanjia!=1){
                ok = false;
                throw new RuntimeException("添加专家失败");
            }

            DeptZhuanjia mmDeptZhuanjia = deptZhuanjiaService.findByZhuanjiaId(ppZhuanjiabiandongDetail.getZhuanjiaid());

            mmDeptZhuanjia.setBiangengshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
            mmDeptZhuanjia.setIsvalid((long)2);

            int modifyDeptZhuanjia = deptZhuanjiaService.updateDeptZhuanjia(mmDeptZhuanjia);
            if (modifyDeptZhuanjia!=1){
                ok = false;
                throw new RuntimeException("修改单位专家失败");
            }

            DeptZhuanjia mmNewDeptZhuanjia = new DeptZhuanjia();
            mmNewDeptZhuanjia.setId(UUID.randomUUID().toString());
            mmNewDeptZhuanjia.setZhuanjiaid(mmZhuanjiaId);
            mmNewDeptZhuanjia.setDeptid(mmOldDeptId);//当前审批单位旧ID
            mmNewDeptZhuanjia.setXingming(mmZhuanjia.getXingming());
            mmNewDeptZhuanjia.setBiangengshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
            mmNewDeptZhuanjia.setIsvalid((long)1);

            int addNewDeptZhuanjia = deptZhuanjiaService.insertDeptZhuanjia(mmNewDeptZhuanjia);
            if (addNewDeptZhuanjia!=1){
                ok = false;
                throw new RuntimeException("添加新单位专家失败");
            }

            ZhuanjiaBiandongHistory mmHistory = new ZhuanjiaBiandongHistory();
            mmHistory.setId(UUID.randomUUID().toString());
            mmHistory.setZhuanjiaid(ppZhuanjiabiandongDetail.getZhuanjiaid());

            if (mmZhuanjia.getZhuanjiazhuangtai()==null){
                mmHistory.setOldzhuantai("");
            }else{
                SysDictData mmDictOldZhuangtai = new SysDictData();
                mmDictOldZhuangtai.setDictType("sys_zhuanjiazhuangtai");
                mmDictOldZhuangtai.setDictValue("10");
                mmHistory.setOldzhuantai(sysDictDataService.selectDictDataList(mmDictOldZhuangtai).get(0).getDictLabel());
            }
            SysDictData mmDictNewZhuangtai = new SysDictData();
            mmDictNewZhuangtai.setDictType("sys_zhuanjiazhuangtai");
            mmDictNewZhuangtai.setDictValue("11");
            mmHistory.setNewzhuangtai(sysDictDataService.selectDictDataList(mmDictNewZhuangtai).get(0).getDictLabel());
            mmHistory.setOlddanwei(deptService.selectDeptById(ppZhuanjiabiandongDetail.getZhuanzidanwei()).getJigoumingcheng());
            mmHistory.setNewdanwei(ppDept.getJigoumingcheng());
            mmHistory.setBiandongshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
            mmHistory.setOrdernum(zhuanjiaBiandongHistoryService.getMaxOrdernum(ppZhuanjiabiandongDetail.getZhuanjiaid())+1);
            mmHistory.setIsvalid((long) 1);

            int addHistory = zhuanjiaBiandongHistoryService.insertZhuanjiaBiandongHistory(mmHistory);
            if (addHistory!=1){
                ok = false;
                throw new RuntimeException("添加专家变动历史失败");
            }

            //转入单位加变动Detail，用于计算
            ZhuanjiaBiandongDetail mmZhuanjiabiandongDetail = new ZhuanjiaBiandongDetail();
            mmZhuanjiabiandongDetail.setId(UUID.randomUUID().toString());
            mmZhuanjiabiandongDetail.setDeptid(mmDeptId);
            mmZhuanjiabiandongDetail.setZhuanjiaid(mmZhuanjiaId);
            mmZhuanjiabiandongDetail.setBiandongriqi(ppZhuanjiabiandongDetail.getBiandongriqi());
            mmZhuanjiabiandongDetail.setZhuanjiamingcheng("专家转入");
            mmZhuanjiabiandongDetail.setShenpishijian(new Date());
            mmZhuanjiabiandongDetail.setBeizhu1("1");
            mmZhuanjiabiandongDetail.setIsused((long)0);
            mmZhuanjiabiandongDetail.setIsvalid((long)1);

            int addBiandongDetail = zhuanjiaBiandongDetailService.insertZhuanjiaBiandongDetailCustom(mmZhuanjiabiandongDetail)==true?1:0;
            if (addBiandongDetail!=1){
                ok = false;
                throw new RuntimeException("添加专家变动详情失败");
            }
        }

        //若不选择认领，
        if (ppIsRenling==0){
            String mmZhuanzhidanwei = ppZhuanjiabiandongDetail.getZhuanzhidanwei()==null?"":ppZhuanjiabiandongDetail.getZhuanzhidanwei();

            //转至单位手填
            if (mmZhuanzhidanweiId.equals("")){

                //1.审核单位通过，且不是专家中心时，创建新任务给下一单位
                if (mmShifoutongguo==1&&!mmDeptId.equals(RuoYiConfig.getZjzxdeptid())){
                    //设置新任务审批单位为本单位的父单位
                    ppRenwu2.setShenpidanweiid(olddeptidToDeptid(deptService.selectDeptById(mmOldDeptId).getParentid()));

                    //通过或驳回都需要创建新任务
                    int addRenwu = this.insertRenwu(ppRenwu2);
                    if (addRenwu!=1){
                        ok = false;
                        throw new RuntimeException("添加任务失败");
                    }
                }

                //2.专家中心通过，将该事务设为待指派，且不再创建任务
                if (mmShifoutongguo==1&&mmDeptId.equals(RuoYiConfig.getZjzxdeptid())){
                    ppShiwu.setLiuchengid("待指派");
                }

                String mmShangyijishenpidanweiid = ppRenwu.getShangyijishenpidanweiid()==null?"":ppRenwu.getShangyijishenpidanweiid();
                //3.若驳回且不是第一个审核单位，则返回上一个审批单位
                if (mmShifoutongguo==2&&!mmShangyijishenpidanweiid.equals("")){
                    ppRenwu2.setShenpidanweiid(ppRenwu.getShangyijishenpidanweiid());
                    //通过或驳回都需要创建新任务
                    int addRenwu = this.insertRenwu(ppRenwu2);
                    if (addRenwu!=1){
                        ok = false;
                        throw new RuntimeException("添加任务失败");
                    }
                }

                //4.若驳回且是第一个审核单位，则退回本人,不创建任务
                if (mmShifoutongguo==2&&mmShangyijishenpidanweiid.equals("")){
                    ppShiwu.setIsqidong((long)0);
                }

            }

            //转至单位选择
            else{
                mmZhuanzhidanweiShangjiId = deptService.selectDeptById(mmZhuanzhidanweiId).getParentid();

                //1.审核单位通过
                if (mmShifoutongguo==1&&!mmOldDeptId.equals(mmZhuanzhidanweiShangjiId)){
                    System.out.println("aaa");

                    //查询转出单位至专家中心所有单位
                    List<String> mmZhuanchuDeptList = deptService.findToZJZX(mmZhuanzidanweiId,new ArrayList<>());
                    //查询转至单位至专家中心所有单位
                    List<String> mmZhuanruDeptList = deptService.findToZJZX(mmZhuanzhidanweiId,new ArrayList<>());

                    List<String> mmDeptList = new ArrayList<>();
                    String mmChongfuXiang = "";
                    int chongfuindex1 = 0;
                    int chongfuindex2 = 0;

                    boolean bk = false;
                    for (int i = 0;i<mmZhuanchuDeptList.size();i++){
                        String tempDeptId = mmZhuanchuDeptList.get(i);
                        for (int j = 0;j<mmZhuanruDeptList.size();j++){
                            String tempDeptId2 = mmZhuanruDeptList.get(j);
                            if (tempDeptId.equals(tempDeptId2)){
                                mmChongfuXiang = tempDeptId;
                                chongfuindex1 = i;
                                chongfuindex2 = j;
                                bk = true;
                                break;
                            }
                        }
                        if (bk){
                            break;
                        }
                    }

                    for (int a = 0;a<chongfuindex1;a++){
                        mmDeptList.add(mmZhuanchuDeptList.get(a));
                    }

                    for (int b = chongfuindex2;b>=0;b--){
                        mmDeptList.add(mmZhuanruDeptList.get(b));
                    }

                    System.out.println("ShenheDeptList:"+mmDeptList);

                    //下一级审批单位
                    int ShenpidanweiIndex = 0;
                    for (int x = 0;x<mmDeptList.size();x++){
                        if (mmDeptList.get(x).equals(mmOldDeptId)){
                            ShenpidanweiIndex = x+1;
                            break;
                        }
                    }

                    //给下一级审批单位添加任务
                    ppRenwu2.setShenpidanweiid(deptidOlddeptidService.OldDeptIdToDeptId(mmDeptList.get(ShenpidanweiIndex)));
                    //通过或驳回都需要创建新任务
                    int addRenwu = this.insertRenwu(ppRenwu2);
                    if (addRenwu!=1){
                        ok = false;
                        throw new RuntimeException("添加任务失败");
                    }
                }

                //2.审核单位通过，且审核单位是转入单位的上级单位,且不再创建任务
                if (mmShifoutongguo==1&&mmOldDeptId.equals(mmZhuanzhidanweiShangjiId)){
                    ppShiwu.setLiuchengid("待指派");
                }

                String mmShangyijishenpidanweiid = ppRenwu.getShangyijishenpidanweiid()==null?"":ppRenwu.getShangyijishenpidanweiid();
                //3.若驳回且不是第一个审核单位，则返回上一个审批单位
                if (mmShifoutongguo==2&&!mmShangyijishenpidanweiid.equals("")){
                    ppRenwu2.setShenpidanweiid(ppRenwu.getShangyijishenpidanweiid());
                    //通过或驳回都需要创建新任务
                    int addRenwu = this.insertRenwu(ppRenwu2);
                    if (addRenwu!=1){
                        ok = false;
                        throw new RuntimeException("添加任务失败");
                    }
                }

                //4.若驳回且是第一个审核单位，则退回本人,不创建任务
                if (mmShifoutongguo==2&&mmShangyijishenpidanweiid.equals("")){
                    ppShiwu.setIsqidong((long)0);
                }
            }
        }

        int modifyDetail = zhuanjiaBiandongDetailService.updateZhuanjiaBiandongDetail(ppZhuanjiabiandongDetail);
        if (modifyDetail!=1){
            ok = false;
            throw new RuntimeException("修改详情失败");
        }

        //修改事务
        int modifyShiwu = shiwuMapper.updateShiwu(ppShiwu);
        if (modifyShiwu!=1){
            throw new RuntimeException("修改事务失败");
        }

        return ok;
    }

    @Override
    @Transactional
    public boolean xiapai(ZhuanjiaBiandongDetail ppZhuanjiabiandongDetail,Shiwu ppShiwu,Dept ppDept,String ppXiapaidanweiId){
        boolean ok = true;

        //创建新任务给下一个审核单位
        Renwu mmRenwu = new Renwu();
        mmRenwu.setRenwuid(UUID.randomUUID().toString());
        mmRenwu.setShiwuid(ppShiwu.getShiwuid());
        mmRenwu.setShangyijishenpidanweiid(deptidOlddeptidService.OldDeptIdToDeptId(ppDept.getId()));
        mmRenwu.setRenwuxiafashijian(new Date());
        mmRenwu.setShifouwancheng((long) 0);
        mmRenwu.setOrdernum(renwuService.findMaxOrdernumByShiwuId(ppShiwu.getShiwuid())+1);
        mmRenwu.setShifoutongguo((long) 0);
        mmRenwu.setIsvalid((long) 1);

        ppZhuanjiabiandongDetail.setZhuanzhidanwei(ppXiapaidanweiId);
        ppZhuanjiabiandongDetail.setZhuanzhidanweimingcheng(deptService.selectDeptById(ppXiapaidanweiId).getJigoumingcheng());
        int modifyDetail = zhuanjiaBiandongDetailService.updateZhuanjiaBiandongDetail(ppZhuanjiabiandongDetail);
        if (modifyDetail!=1){
            ok = false;
            throw new RuntimeException("修改专家变动详情失败");
        }

        //转入单位新ID
        String mmZhuanrudanweiId = deptidOlddeptidService.OldDeptIdToDeptId(ppXiapaidanweiId);
        mmRenwu.setShenpidanweiid(mmZhuanrudanweiId);

        //通过或驳回都需要创建新任务
        int addRenwu = this.insertRenwu(mmRenwu);
        if (addRenwu!=1){
            ok = false;
            throw new RuntimeException("添加任务失败");
        }

        ppShiwu.setLiuchengid("待接收");
        //修改事务
        int modifyShiwu = shiwuMapper.updateShiwu(ppShiwu);
        if (modifyShiwu!=1){
            throw new RuntimeException("修改事务失败");
        }

        return ok;
    }

    @Override
    @Transactional
    public boolean jieshou(ZhuanjiaBiandongDetail ppZhuanjiabiandongDetail,Renwu ppRenwu,Renwu ppRenwu2,Shiwu ppShiwu,Dept ppDept,long ppShifoutongguo){
        boolean ok = true;

        //修改本次任务
        int modifyRenwu = this.updateRenwu(ppRenwu);
        if (modifyRenwu!=1){
            ok = false;
            throw new RuntimeException("修改任务失败");
        }

        //是否通过
        long mmShifoutongguo = ppRenwu.getShifoutongguo();
        //当前审批单位
        String mmDeptId = ppRenwu.getShenpidanweiid();
        //当前审批单位旧ID
        String mmOldDeptId = ppDept.getId();
        //转出单位旧ID
        String mmZhuanzidanweiId = ppZhuanjiabiandongDetail.getZhuanzidanwei();
        //转出单位新ID

        //转入单位旧ID
        String mmZhuanzhidanweiId = ppZhuanjiabiandongDetail.getZhuanzhidanwei()==null?"":ppZhuanjiabiandongDetail.getZhuanzhidanwei();
        //转入单位新ID

        //转入单位上级旧ID
        String mmZhuanzhidanweiShangjiId = "";

        if (ppShifoutongguo==1){
            ppZhuanjiabiandongDetail.setShenpishijian(new Date());
            ppZhuanjiabiandongDetail.setBeizhu1("1");//通过标记
            int modifyDetail = zhuanjiaBiandongDetailService.updateZhuanjiaBiandongDetail(ppZhuanjiabiandongDetail);
            if (modifyDetail!=1){
                ok = false;
                throw new RuntimeException("修改详情失败");
            }

            Zhuanjia mmZhuanjia = zhuanjiaService.selectZhuanjiaByZhuanjiaid(ppZhuanjiabiandongDetail.getZhuanjiaid());

            mmZhuanjia.setZhuangtaibiandongshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
            mmZhuanjia.setZhuanjiazhuangtai("8");//转出

            int modifyZhuanjia = zhuanjiaService.updateZhuanjia(mmZhuanjia);
            if (modifyZhuanjia!=1){
                ok = false;
                throw new RuntimeException("修改专家失败");
            }

            Zhuanjia mmNewZhuanjia = mmZhuanjia;
            String mmZhuanjiaId = UUID.randomUUID().toString();
            mmNewZhuanjia.setZhuanjiaid(mmZhuanjiaId);
            mmNewZhuanjia.setZhuanjiazhuangtai("11");
            mmNewZhuanjia.setZhuangtaibiandongshijian(ppZhuanjiabiandongDetail.getBiandongriqi());

            int addZhuanjia = zhuanjiaService.insertZhuanjia(mmNewZhuanjia);
            if (addZhuanjia!=1){
                ok = false;
                throw new RuntimeException("添加专家失败");
            }

            DeptZhuanjia mmDeptZhuanjia = deptZhuanjiaService.findByZhuanjiaId(ppZhuanjiabiandongDetail.getZhuanjiaid());

            mmDeptZhuanjia.setBiangengshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
            mmDeptZhuanjia.setIsvalid((long)2);

            int modifyDeptZhuanjia = deptZhuanjiaService.updateDeptZhuanjia(mmDeptZhuanjia);
            if (modifyDeptZhuanjia!=1){
                ok = false;
                throw new RuntimeException("修改单位专家失败");
            }

            DeptZhuanjia mmNewDeptZhuanjia = new DeptZhuanjia();
            mmNewDeptZhuanjia.setId(UUID.randomUUID().toString());
            mmNewDeptZhuanjia.setZhuanjiaid(mmZhuanjiaId);
            mmNewDeptZhuanjia.setDeptid(mmOldDeptId);//当前审批单位旧ID
            mmNewDeptZhuanjia.setXingming(mmZhuanjia.getXingming());
            mmNewDeptZhuanjia.setBiangengshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
            mmNewDeptZhuanjia.setIsvalid((long)1);

            int addNewDeptZhuanjia = deptZhuanjiaService.insertDeptZhuanjia(mmNewDeptZhuanjia);
            if (addNewDeptZhuanjia!=1){
                ok = false;
                throw new RuntimeException("添加新单位专家失败");
            }

            ZhuanjiaBiandongHistory mmHistory = new ZhuanjiaBiandongHistory();
            mmHistory.setId(UUID.randomUUID().toString());
            mmHistory.setZhuanjiaid(ppZhuanjiabiandongDetail.getZhuanjiaid());

            if (mmZhuanjia.getZhuanjiazhuangtai()==null){
                mmHistory.setOldzhuantai("");
            }else{
                SysDictData mmDictOldZhuangtai = new SysDictData();
                mmDictOldZhuangtai.setDictType("sys_zhuanjiazhuangtai");
                mmDictOldZhuangtai.setDictValue("10");
                mmHistory.setOldzhuantai(sysDictDataService.selectDictDataList(mmDictOldZhuangtai).get(0).getDictLabel());
            }
            SysDictData mmDictNewZhuangtai = new SysDictData();
            mmDictNewZhuangtai.setDictType("sys_zhuanjiazhuangtai");
            mmDictNewZhuangtai.setDictValue("11");
            mmHistory.setNewzhuangtai(sysDictDataService.selectDictDataList(mmDictNewZhuangtai).get(0).getDictLabel());
            mmHistory.setOlddanwei(deptService.selectDeptById(ppZhuanjiabiandongDetail.getZhuanzidanwei()).getJigoumingcheng());
            mmHistory.setNewdanwei(ppDept.getJigoumingcheng());
            mmHistory.setBiandongshijian(ppZhuanjiabiandongDetail.getBiandongriqi());
            mmHistory.setOrdernum(zhuanjiaBiandongHistoryService.getMaxOrdernum(ppZhuanjiabiandongDetail.getZhuanjiaid())+1);
            mmHistory.setIsvalid((long) 1);

            int addHistory = zhuanjiaBiandongHistoryService.insertZhuanjiaBiandongHistory(mmHistory);
            if (addHistory!=1){
                ok = false;
                throw new RuntimeException("添加专家变动历史失败");
            }

            //转入单位加变动Detail，用于计算
            ZhuanjiaBiandongDetail mmZhuanjiabiandongDetail = new ZhuanjiaBiandongDetail();
            mmZhuanjiabiandongDetail.setId(UUID.randomUUID().toString());
            mmZhuanjiabiandongDetail.setDeptid(mmDeptId);
            mmZhuanjiabiandongDetail.setZhuanjiaid(mmZhuanjiaId);
            mmZhuanjiabiandongDetail.setBiandongriqi(ppZhuanjiabiandongDetail.getBiandongriqi());
            mmZhuanjiabiandongDetail.setZhuanjiamingcheng("专家转入");
            mmZhuanjiabiandongDetail.setShenpishijian(new Date());
            mmZhuanjiabiandongDetail.setBeizhu1("1");
            mmZhuanjiabiandongDetail.setIsused((long)0);
            mmZhuanjiabiandongDetail.setIsvalid((long)1);

            int addBiandongDetail = zhuanjiaBiandongDetailService.insertZhuanjiaBiandongDetailCustom(mmZhuanjiabiandongDetail)==true?1:0;
            if (addBiandongDetail!=1){
                ok = false;
                throw new RuntimeException("添加专家变动详情失败");
            }
        }
        else if (ppShifoutongguo==0) {
            int addRenwu = renwuService.insertRenwu(ppRenwu2);
            if (addRenwu!=1){
                throw new RuntimeException("修改事务失败");
            }
        }

        //修改事务
        int modifyShiwu = shiwuMapper.updateShiwu(ppShiwu);
        if (modifyShiwu!=1){
            throw new RuntimeException("修改事务失败");
        }

        return ok;
    }

    @Override
    public List<Map<String,Object>> findShenhejiluByShiwuid(String ppShiwuId){
        return renwuMapper.findShenhejiluByShiwuid(ppShiwuId);
    }

    @Override
    public List<Map<String,Object>> findShenheListByDept(String ppShenpidanweiid,int ppShenhezhuangtai,String ppJigoumingcheng,
                                                         String ppZhuanjiaxingming,String ppRenwuxiafashijian1,String ppRenwuxiafashijian2,
                                                         String ppRenwuwanchengshijian1,String ppRenwuwanchengshijian2,String biandongleixing,int ppPageIndex,int ppPageSize){

        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
        ppJigoumingcheng = "%"+ppJigoumingcheng+"%";
        ppZhuanjiaxingming = "%"+ppZhuanjiaxingming+"%";
        return renwuMapper.findShenheListByDept(ppShenpidanweiid,ppShenhezhuangtai,ppJigoumingcheng,ppZhuanjiaxingming,ppRenwuxiafashijian1,ppRenwuxiafashijian2,
                ppRenwuwanchengshijian1,ppRenwuwanchengshijian2,biandongleixing,mmStartIndex,ppPageSize);
    }

    public int countShenheListByDept(String ppShenpidanweiid,int ppShenhezhuangtai,String ppJigoumingcheng,
                                     String ppZhuanjiaxingming,String ppRenwuxiafashijian1,String ppRenwuxiafashijian2,
                                     String ppRenwuwanchengshijian1,String ppRenwuwanchengshijian2,String biandongleixing){

        ppJigoumingcheng = "%"+ppJigoumingcheng+"%";
        ppZhuanjiaxingming = "%"+ppZhuanjiaxingming+"%";

        return renwuMapper.countShenheListByDept(ppShenpidanweiid,ppShenhezhuangtai,ppJigoumingcheng,ppZhuanjiaxingming,ppRenwuxiafashijian1,ppRenwuxiafashijian2,
                ppRenwuwanchengshijian1, ppRenwuwanchengshijian2,biandongleixing);
    }

    @Override
    public List<Map<String,Object>> findZhuanliShenheListByDept(String ppShenpidanweiid,int ppShenhezhuangtai,String ppJigoumingcheng,
                                                                String ppZhuanjiaxingming,String ppRenwuxiafashijian1,String ppRenwuxiafashijian2,
                                                                String ppRenwuwanchengshijian1,String ppRenwuwanchengshijian2,int ppPageIndex,int ppPageSize){
        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
        ppJigoumingcheng = "%"+ppJigoumingcheng+"%";
        ppZhuanjiaxingming = "%"+ppZhuanjiaxingming+"%";
        return renwuMapper.findZhuanliShenheListByDept(ppShenpidanweiid,ppShenhezhuangtai,ppJigoumingcheng,ppZhuanjiaxingming,
                ppRenwuxiafashijian1,ppRenwuxiafashijian2,ppRenwuwanchengshijian1,ppRenwuwanchengshijian2,mmStartIndex,ppPageSize);
    }

    public int countZhuanliShenheListByDept(String ppShenpidanweiid,int ppShenhezhuangtai,String ppJigoumingcheng,
                                     String ppZhuanjiaxingming,String ppRenwuxiafashijian1,String ppRenwuxiafashijian2,
                                            String ppRenwuwanchengshijian1,String ppRenwuwanchengshijian2){

        ppJigoumingcheng = "%"+ppJigoumingcheng+"%";
        ppZhuanjiaxingming = "%"+ppZhuanjiaxingming+"%";

        return renwuMapper.countZhuanliShenheListByDept(ppShenpidanweiid,ppShenhezhuangtai,ppJigoumingcheng,ppZhuanjiaxingming,
                ppRenwuxiafashijian1,ppRenwuxiafashijian2, ppRenwuwanchengshijian1,ppRenwuwanchengshijian2);
    }

    @Override
    public long findMaxOrdernumByShiwuId(String ppShiwuId){
        Object mmMaxOrdernum = renwuMapper.findMaxOrdernumByShiwuId(ppShiwuId);
        return mmMaxOrdernum==null?0:Long.parseLong(mmMaxOrdernum.toString());
    }

    //新旧单位ID转换
    public String deptidToOlddeptid(String ppDeptId){
        return deptidOlddeptidService.selectDeptidOlddeptidByDeptid(ppDeptId).getOlddeptid();
    }

    public String olddeptidToDeptid(String ppOldDeptId){
        DeptidOlddeptid mmOne = new DeptidOlddeptid();
        mmOne.setOlddeptid(ppOldDeptId);
        return deptidOlddeptidService.selectDeptidOlddeptidList(mmOne).get(0).getDeptid();
    }
}
